import request from './request'

/**
 * 微信授权拿Code  window.location.href
 */
const wxAuthUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx33d4eaf790edb175&redirect_uri=${encodeURIComponent(window.location.href)}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`

/**
 * 活动详情
 * @param {id}} id 
 * @returns 
 */
const getActData = (id) => {
    return request.get(`/admin/activity/front/read?id=${id}`)
}
/**
 * 生成二唯码
 * @param {*} url 
 * @param {*} id 
 * @returns 
 */
const createActivityQCode = (url = window.location.href, activityId = null, homePicUrl = null, desc = null) => {
    const params = {
        url,
        activityId,
        homePicUrl,
        desc
    }
    return request.post(`/wx/activity/generateQCode`, params)
}

/**
 * 添加统计\报名\分享数
 * @param {*} id   type： 1.浏览数 2.报名数 3.分享数 4.成团数
 * @param {*} type 
 * @returns 
 */
const addStatistics = (id, type = 1) => {
    return request.get(`/admin/activity/front/statistics?activityId=${id}&type=${type}`)
}

/**
 * 查询团列表与团列表
 * @param {*} data 
 * @returns 
 */

const getGroupList = (data) => {
    return request.post(`/wx/activity/grouponList`, Object.assign({
        tag: 1,
        page: 1,
        limit: 10,
        userName: '',
        grouponCode: ''
    }, data))
}

/**
 * 登录与注册
 * @param {*} code 
 * @param {*} userInfo   wx/auth/loginAndRegister?code=071ocwGa1z6fIA0zbrFa119x8U1ocwGq
 * @returns 
 */
const loginAndRegister = (code) => {
    return request.get(`/wx/auth/loginAndRegister?code=${code}`)
}

/**
 * 生成订单-> orderId
 * {
    "activityId":1,
    "grouponId":"", //开团传空；参团必传
    "grouponCode": "", //开团传空；参团必传
    "subject":"唱歌",
    "contactPhone":"13713813920",
    "userType":"1", // 1老学员 2新学员
    "userAge":8,
    "message":"测试订单"
}
 * @param {*} params 
 * @returns 
 */
const orderSubmit = (params) => {
    return request.post('/wx/order/act/submit', params)
}

/**
 * 生成微信订单
 * @param {*} orderId 
 * @returns 
 */
const createWXPay = (orderId) => {
    return request.post('/wx/order/prepay', {
        orderId
    })
}

const getWXSignature = () => {
    return request.get('/wx/getJsapiSignature')
}

export {
    wxAuthUrl,
    getActData,
    createActivityQCode,
    addStatistics,
    getGroupList,
    loginAndRegister,
    orderSubmit,
    createWXPay,
    getWXSignature
}